package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.orgs;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Organization;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository.DataGenerator;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.PresenterInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.UiInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseFragment;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseUiView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.CommonAdapter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.base.ViewHolder;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.CommonErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.ProfileContact;
import com.graduationdesign.zengyan.graduationdesignapp.ui.presenter.ProfilePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ZengYan on 2018/4/22.
 *
 * @desc :
 */
public class MyOrgsFragment extends BaseFragment implements ProfileContact.ShowOrgsIJoin {

    @BindView(R.id.recycler_view)
    RecyclerView mRecylerview;

    @BindView(R.id.base_ui)
    BaseUiView baseUiView;


    private List<Organization> mList = new ArrayList<>();
    private CommonAdapter<Organization> mAdapter;
    private ProfileContact.Presenter presenter;

    @Override
    protected int provideLayoutId() {
        return R.layout.orgs_layout;
    }

    @Override
    protected String provideTag() {
        return "MyOrgsFragment";
    }

    @Override
    protected void initData() {
        BaseErrorView mErrorView = new CommonErrorView(getActivity());
        presenter = PresenterInjection.provideProfilePresenter(
                ProfilePresenter.newBuilder()
                        .setShowOrgsIJoin(this)
                        .setMLoadingView(baseUiView)
                        .setBaseErrorView(mErrorView)
        );
        presenter.loadOrgsIJoinIn("201430098034",baseUiView);

    }

    @Override
    protected void initView(View view) {

        mRecylerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecylerview.setNestedScrollingEnabled(false);
        List<Integer> items = DataGenerator.getNatureImages(getContext());
        mAdapter = new CommonAdapter<Organization>(getActivity(), R.layout.item_orgs, mList) {

            @Override
            protected void convert(ViewHolder holder, Organization organization, int position) {
                holder.setText(R.id.title, organization.getName());
                holder.setText(R.id.sub_title, organization.getDescription());
                int index = DataGenerator.randInt(items.size()-1);
                holder.setImageResource(R.id.bg, items.get(index));
                holder.setOnClickListener(R.id.card, v -> {
                    Organization organization1 = mList.get(position);
                    Intent intent = UiInjection.provideOrgPersons(getActivity(), organization1.getOrganization_id());
                    getActivity().startActivity(intent);
                });
            }
        };

        mRecylerview.setAdapter(mAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.unSubsrible();
    }

    @Override
    public void showOrgsIJoin(List<Organization> list) {
        mList.clear();
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
        baseUiView.setVisibility(View.GONE);
    }
}
